#include "WidgetButton.h"
#include "Menu.h"
#include "RessourceManager.h"

WidgetButton::WidgetButton(Menu* menu, int x, int y, int w, int h, std::string text, int textSize) : Widget(x,y)
{
    this->menu = menu;
    this->w = w;
    this->h = h;
    this->text.setString(text);
    this->text.setCharacterSize(textSize);
    this->text.setFont(*RessourceManager::getFont("font.ttf"));
    this->text.setPosition(x+(w/2)-(this->text.getGlobalBounds().width/2),y+(h/2)-(textSize/2));
    this->textSize = textSize;
    this->box.setSize(sf::Vector2f(w,h));
    this->box.setPosition(x,y);
    this->box.setOutlineThickness(2);

    hover = false;
    updateGfx();

}

void WidgetButton::onEvent(sf::Event* event)
{
    if(event->type == sf::Event::MouseMoved)
    {
        if(event->mouseMove.x >= x && event->mouseMove.x <= x+w && event->mouseMove.y >= y && event->mouseMove.y <= y+h)
        {
            if(!hover)
            {
                hover = true;
                updateGfx();
            }
        }
        else
        {
            if(hover)
            {
                hover = false;
                updateGfx();
            }
        }
    }

    if(event->type == sf::Event::MouseButtonPressed && event->mouseButton.button == sf::Mouse::Left)
    {
        if(hover)
            menu->onButtonPressed(this);
    }
}

void WidgetButton::onRender(sf::RenderWindow* window)
{
    window->draw(box);
    window->draw(text);
}

void WidgetButton::updateGfx()
{
    if(!hover)
    {
        text.setColor(sf::Color(255,255,255,150));
        box.setFillColor(sf::Color(0,0,0,150));
        box.setOutlineColor(sf::Color(255,255,255,150));
    }
    else
    {
        text.setColor(sf::Color(255,255,255,255));
        box.setFillColor(sf::Color(0,0,0,255));
        box.setOutlineColor(sf::Color(255,255,255,255));
    }
}

WidgetButton::~WidgetButton()
{
    //dtor
}
